from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.contrib import admin

class Account(models.Model):
    ACCOUNT_TYPE = (
        ('B', _('balance')),
        ('R', _('results')),
    )
    ACCOUNT_NATURE = (
        ('C', _('creditor')),
        ('D', _('debtor')),
    )
    account_no = models.CharField(_('account'), max_length=16, primary_key=True)
    description = models.CharField(_('description'), max_length=50)
    type = models.CharField(_('type'), max_length=1, choices=ACCOUNT_TYPE)
    nature = models.CharField(_('nature'), max_length=1, choices=ACCOUNT_NATURE)
    initial_value = models.DecimalField(_('initial value'),max_digits=14, decimal_places=2, blank=True, null=True, editable=False)
    credit = models.DecimalField(_('credit'),max_digits=14, decimal_places=2, blank=True, null=True, editable=False)
    debit = models.DecimalField(_('debit'),max_digits=14, decimal_places=2, blank=True, null=True, editable=False)
    def __unicode__(self):
        return '%s %s' % (self.account_no, self.description)
    class Meta:
        ordering = ["account_no"]
        verbose_name = _('account')
        verbose_name_plural = _('accounts')
    
    
admin.site.register(Account)

class Journal_type(models.Model):
    id = models.CharField(_('id'), max_length=2, primary_key=True)
    name = models.CharField(_('name'), max_length=30)
    def __unicode__(self):
        return self.name
    class Meta:
        ordering = ["id"]
        verbose_name = _('journal type')
        verbose_name_plural = _('journal types')
    
admin.site.register(Journal_type)
    
class Journal(models.Model):
    JOURNAL_STATUS = (
        ('A', _('active')),
        ('N', _('inactive')),
        ('C', _('cancel')),
        ('U', _('unbalance')),
    )
    year = models.SmallIntegerField(_('year'))
    period = models.SmallIntegerField(_('period'))
    sequence = models.IntegerField(_('sequence'))
    reference = models.CharField(_('reference'), max_length=10)
    status = models.CharField(_('status'), max_length=1, choices=JOURNAL_STATUS)
    type = models.ForeignKey(Journal_type, verbose_name=_('journal type'))
    date = models.DateTimeField(_('date'))
    credit = models.DecimalField(_('credit'),max_digits=14, decimal_places=2, blank=True, null=True, editable=False)
    debit = models.DecimalField(_('debit'),max_digits=14, decimal_places=2, blank=True, null=True, editable=False)
    def __unicode__(self):
        return '%d %d %d %s %s' % (self.year, self.period, self.sequence, self.reference, self.status)
    class Meta:
        ordering = ["year", "period", "sequence"]
        verbose_name = _('journal')
        verbose_name_plural = _('journals')
    

class JournalDetail(models.Model):
    journal = models.ForeignKey(Journal, edit_inline=models.TABULAR)
    reference = models.CharField(_('reference'), max_length=10)
    account_no = models.ForeignKey(Account, verbose_name= _('account'))
    concept = models.CharField(_('concept'), max_length=40)
    credit = models.DecimalField(_('credit'),max_digits=14, decimal_places=2, blank=True, null=True)
    debit = models.DecimalField(_('debit'),max_digits=14, decimal_places=2, blank=True, null=True)
    class Meta:
        verbose_name = _('journal detail')
        verbose_name_plural = _('journal details')
    
class JournalDetailInline(admin.TabularInline):
    model = JournalDetail
    extra = 1

class JournalAdmin(admin.ModelAdmin):
    inlines = [JournalDetailInline]

admin.site.register(Journal,JournalAdmin)
